using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace ForumExplorer.Extension.Tagging
{
    public partial class TaggingPanel : UserControl
    {
        public Tagging taggingService;
        private int postID;
        private string currentSearch;
        /// <revisionHistory>
        /// <revision author="Moayad Mardini [mardini]" date="26-August-2006">
        /// Show the login form as a dialog form instead of a normal form
        /// </revision>
        /// </revisionHistory>
        public TaggingPanel()
        {
            InitializeComponent();
            taggingService = new Tagging();
            currentSearch = String.Empty;
            //Setup the TaggingPanel with the correct tagging information
        }

        /// <summary>
        /// Populate the panel with a list of the tags for the given URL
        /// </summary>
        private void SetupPanel(int threadID)
        {
            if (taggingService.isLoggedIn)
            {
                radioButtonTagsforThread.Checked = true;
                groupBox2.Text = "Tags for Thread:";
                string[] tagList = taggingService.GetTagsForThreadID(threadID);
                listBoxMyTags.Items.Clear();
                foreach (string s in tagList)
                {
                    listBoxMyTags.Items.Add(s);
                }
                if (listBoxTopTags.Items.Count == 0)
                {
                    tagList = taggingService.GetTopTags();
                    foreach (string s in tagList)
                    {
                        listBoxTopTags.Items.Add(s);
                    }
                }
            }
            else
            {
                listBoxMyTags.Items.Clear();
                listBoxMyTags.Items.Add("Please log in");
                listBoxMyTags.Enabled = false;
            }
        }
        
        //this method will update the Del.icio.us panel based upon the current loaded document in the webBrowser control
        //If the current document loaded in the web browser is not a question thread, the panel will be greyed out
        //Parameters:  string url - should be the url of the currently loaded document
        public void RefreshData(string url)
        {
            ForumExplorer.Core.ObjectModel.ForumUtility util = new ForumExplorer.Core.ObjectModel.ForumUtility();
            postID = util.GetPostIDFromURL(url);

            //if you are logged in...enable the control
            //check and see if the webBrowser is currently on a question thread on the forums
            //if it's not, then disable the control
            this.Enabled = true;
            if (postID == 0)
            {
                this.listBoxMyTags.Items.Clear();
                this.listBoxMyTags.Items.Add("Not currently on a thread.");
                this.listBoxMyTags.Enabled = false;
            }
            else
            {
                this.listBoxMyTags.Items.Clear();
                this.listBoxMyTags.Enabled = true;
                SetupPanel(postID);
            }
        }

        //private void buttonTagIt_Click_1(object sender, EventArgs e)
        //{
        //    if (textBoxNewTag.Text != "")
        //    {
        //        taggingService.TagThread(this.textBoxNewTag.Text, postID.ToString());
        //        MessageBox.Show("Thread successfully tagged \"" + this.textBoxNewTag.Text + "\".");
        //    }
        //    else if (listBoxMyTags.Focused)
        //    {
        //        MessageBox.Show("WOW!");
        //    }
        //}

        private void button1_Click(object sender, EventArgs e)
        {
            currentSearch = textBoxNewTag.Text;
            ViewSearch();
        }

        private void ViewSearch()
        {
            if (currentSearch != "")
            {
                this.groupBox2.Text = "Threads with tag \"" + currentSearch + "\"";
				taggingService.GetThreadIDsForTagCompleted += new EventHandler<Tagging.GetThreadIDsEventArgs>(taggingService_GetThreadIDsForTagCompleted);
				button1.Enabled = false;
                taggingService.GetThreadIDsforTag(currentSearch);
            }
            else
                ViewTags();
                
        }

		void taggingService_GetThreadIDsForTagCompleted ( object sender, Tagging.GetThreadIDsEventArgs e )
		{
			if (this.InvokeRequired)
			{
				this.BeginInvoke(new EventHandler<Tagging.GetThreadIDsEventArgs>(taggingService_GetThreadIDsForTagCompleted), new Object[] { sender, e });
			}
			else
			{
				try
				{
					int[] results = e.IDs;
					listBoxMyTags.Items.Clear();
					listBoxMyTags.Enabled = true;
					radioButtonSearchResults.Checked = true;
					foreach (int i in results)
					{
						if (!listBoxMyTags.Items.Contains(i))
							listBoxMyTags.Items.Add(i);
					}
					listBoxMyTags.Refresh();
				}
				finally
				{
					button1.Enabled = true;
				}
			}
		}

        private void ViewTags()
        {
            SetupPanel(postID);
        }

        public delegate void indexChangedDelegate(object sender, EventArgs e, int postID);
        public event indexChangedDelegate listBoxIndexChanged;

        private void listBoxMyTags_SelectedIndexChanged(object sender, EventArgs e)
        {
            if ((this.groupBox2.Text.Contains("Threads with tag")) && (this.listBoxMyTags.Text != ""))
                this.listBoxIndexChanged(sender, e, Convert.ToInt32(this.listBoxMyTags.Text));
            else
                textBoxNewTag.Text = listBoxMyTags.Text;
        }

        private void listBoxMyTags_DoubleClick(object sender, EventArgs e)
        {
            if (this.groupBox2.Text.Contains("Tags for"))
            {
                currentSearch = (string)listBoxMyTags.SelectedItem;
                ViewSearch();
            }
        }

        private void radioButtonSearchResults_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonSearchResults.Checked)
                ViewSearch();
            else
                ViewTags();
        }

        //private void listBoxTopTags_DoubleClick(object sender, EventArgs e)
        //{
        //    if (postID != 0)
        //    {
        //        DialogResult r = MessageBox.Show("Would you like to tag this tag as \"" + (string)listBoxTopTags.SelectedItem + "\"?", "Tag This Thread?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
        //        if (r == DialogResult.Yes)
        //        {
        //            taggingService.TagThread((string)listBoxTopTags.SelectedItem, postID.ToString());
        //            MessageBox.Show("Thread successfully tagged \"" + (string)listBoxTopTags.SelectedItem + "\".");
        //        }
        //    }
        //    else
        //    {
        //        currentSearch = (string)listBoxTopTags.SelectedItem;
        //        ViewSearch();
        //    }
        //}

        private void listBoxTopTags_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBoxNewTag.Text = listBoxTopTags.Text;
        }
    }
}
